package com.yiyi.controller;

import com.yiyi.entity.*;
import com.yiyi.form.AddResultForm;
import com.yiyi.form.CorrectPaperForm;
import com.yiyi.service.*;
import com.yiyi.util.PackageVOUtil;
import com.yiyi.vo.R;
import com.yiyi.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yiyi
 * @date 2021/5/30
 *
 * 1、保存考试结果
 * 2、批改试卷
 * 3、通过申请者id和岗位id获取考试详细结果
 */
@RestController
@RequestMapping("/result")
@Api(tags = {"考试结果操作接口"})
public class ResultController {

    @Autowired
    private ResultService resultService;
    @Autowired
    private ExamService examService;
    @Autowired
    private ExamQuestionService examQuestionService;
    @Autowired
    private ChoiceService choiceService;
    @Autowired
    private AnswerService answerService;

    /**
     * 保存考试结果
     *
     * @param addResultForm
     * @return
     * */
    @PostMapping("/save")
    @ApiOperation(value = "新增考试结果")
    public R save(@RequestBody AddResultForm addResultForm) {
        // 保存结果主体内容，返回结果id
        String resultId = resultService.saveResult(addResultForm);
        // 自动生成选择题分数
        // 根据试卷id获取题目id列表
        List<String> choiceIds = examQuestionService.findByExamId(addResultForm.getExamId(), 0);
        // 根据题目id列表获取真实答案
        List<String> realKey = choiceService.findKeyByIds(choiceIds);
        // 根据题目id列表获取题目分数
        List<Integer> choiceGrade = choiceService.findGradeByIds(choiceIds);
        resultService.saveChoiceGrade(resultId, realKey, choiceGrade);
        return R.ok();
    }

    /**
     * 批改试卷
     *
     * @param correctPaperForm
     * @return
     * */
    @PostMapping("/correctPaper")
    @ApiOperation(value = "批改试卷")
    public R correctPaper(@RequestBody CorrectPaperForm correctPaperForm) {
        int flag = resultService.saveGrade(correctPaperForm);
        if (flag != 0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 通过用户id和岗位id获取考试详细结果
     *
     * @param userId
     * @param positionId
     * @return
     * */
    @GetMapping("/findResultByTwoId/{userId}/{positionId}")
    @ApiOperation(value = "通过用户id和岗位id获取考试详细结果")
    public R findResultByTwoId(@PathVariable("userId") String userId, @PathVariable("positionId") String positionId) {
        // 获取该岗位对应的试卷id列表
        List<String> examIds = examService.findIdsByPositionId(positionId);
        // 首先获取结果信息
        Result result = resultService.findByUerIdAndExamIds(userId, examIds);
        if (result == null) {
            return R.fail();
        }
        // 其次获取试卷信息
        Exam exam = examService.findById(result.getExamId());
        List<String> choiceIds = examQuestionService.findByExamId(result.getExamId(), 0);
        List<String> answerIds = examQuestionService.findByExamId(result.getExamId(), 1);
        List<Choice> choices = choiceService.findListByIds(choiceIds);
        List<Answer> answers = answerService.findListByIds(answerIds);
        // 组装返回结果
        ResultVO resultVO = PackageVOUtil.packageResultVO(result, exam, choices, answers);
        return R.ok(resultVO);
    }
}
